package com.ruoyi.project.system.nhmtcnn.controller;


import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.nhmtcnn.domain.FaceDetectionInfo;
import com.ruoyi.project.system.nhmtcnn.service.impl.NHmtcnnServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.base_family_info.domain.BaseFamilyInfo;
import com.ruoyi.project.system.base_family_info.service.IBaseFamilyInfoService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.farmer.domain.Farmer;
import com.ruoyi.project.system.farmer.service.IFarmerService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static cn.hutool.core.lang.Console.print;
import static com.github.pagehelper.page.PageMethod.startPage;

@Controller
@RequestMapping("/face_detection")
public class NhMtcnnController extends BaseController{

    @Autowired
    private NHmtcnnServiceImpl nhmtcnnService;
    @Autowired
    private IFarmerService farmerService;
    /**
     * 识别农户姓名
     */
    @PostMapping("/img_face")
    @ResponseBody
    public AjaxResult runPythonScript(@RequestParam("image") MultipartFile imageFile) {
        if (imageFile.isEmpty()) {
            return AjaxResult.error("未上传文件");
        }

        try {
            // 设置文件保存的路径
            String folderPath = "D:\\NH\\Img_face\\"; // 确保路径以 "\\" 结尾
            String filename = imageFile.getOriginalFilename();
            Path path = Paths.get(folderPath + filename);

            // 确保目录存在
            Files.createDirectories(path.getParent());

            // 将文件保存到服务器，如果文件已存在，将其替换
            Files.copy(imageFile.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);

            // 构建文件的完整路径用于返回
            String filePath = folderPath + filename;

            FaceDetectionInfo faceDetectionInfo = new FaceDetectionInfo(filePath);
            FaceDetectionInfo faceDetectedInfo = nhmtcnnService.detectImgByFaceNet(filePath);
            if (faceDetectedInfo.getExitCode() == 1) {
                return AjaxResult.error("人脸识别结果失败！");
            } else {
                if (!faceDetectedInfo.getName().isEmpty()) {
                    if (faceDetectedInfo.getName().equals("chen_xiuhao\n")){
                        faceDetectedInfo.setName("刘玉平");
                    }
                    faceDetectionInfo.setName(faceDetectedInfo.getName());
                } else {
                    faceDetectionInfo.setName("奇了怪了");
                }
            }

            // 返回包含文件路径的响应
            return AjaxResult.success("文件上传成功", faceDetectionInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("文件上传失败");
        }
    }

    /**
     * 查询农户个人信息
     */
    @PostMapping ("/getFarmerByName")
    @ResponseBody
    public Farmer getFarmerByName(@RequestParam String Name) {
        return nhmtcnnService.selectFarmByName(Name);
    }
}

